package lg.minecraft.plugin.pokemonmenu.command;

import lg.minecraft.plugin.cobblemoncore.util.PokemonDataCache;
import lg.minecraft.plugin.cobblemoncore.util.PokemonPropertiesArgumentType;
import lg.minecraft.plugin.library.command.CommandContext;
import lg.minecraft.plugin.library.command.CommandType;
import lg.minecraft.plugin.library.command.LGCommand;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.handler.input.PlayerInputManager;
import org.bukkit.entity.Player;

import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class InputCommand extends LGCommand {

    public InputCommand() {
        super(LGPokemonMenu.getInstance(), "input");
    }

    @Override
    public CommandType getCommandType() {
        return CommandType.PLAYER;
    }

    @Override
    public void onCommand(CommandContext context) {
        Player player = context.getPlayer();
        StringBuilder builder = new StringBuilder();
        for (String arg : context.getArgs()) {
            builder.append(arg).append(" ");
        }
        if (PlayerInputManager.isWaitingForInput(player)) {
            PlayerInputManager.handleInput(player, builder.toString().trim());
        }
    }

    @Override
    public List<String> onTabComplete(CommandContext context) {
        Player player = context.getPlayer();
        InputType inputType = PlayerInputManager.getPendingInputType(player);
        if (inputType == null) return null;
        if (inputType == InputType.POKEMON_PROPERTIES) {
            String[] array = context.getArgs();
            return PokemonPropertiesArgumentType.INSTANCE.listSuggestions(array);
        } else if (inputType == InputType.POKEMON_MOVE) {
            return PokemonDataCache.getInstance().getMoveKeyList();
        }
        return List.of();
    }

}
